import numpy as np
# import matplotlib.pyplot as plt
# import cv2
#
# img = cv2.imread('cat.jpg')
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#
# h, w = img.shape[0], img.shape[1]
# fig1 = plt.figure('原始图像')
# plt.imshow(img)
# plt.axis('off')
# plt.title('Original image')
# print( '\t\t\t   原始图像形状:\n', '\t\t\t', img.shape )


# import cv2
# img = cv2.imread("Lena.Bmp")
# cv2.namedWindow("Image")    #定义一个Image的窗口
# cv2.imshow("Image", img)    #展示图像
# print("图像大小为：",img.shape)    #图像大小（256，256，3）行数，列数，通道数
# width=img.shape[1]
# heigt= img.shape[0]
# mean = cv2.mean(img)[0]
# #print(mean)
# (mean , stddv) = cv2.meanStdDev(img)
# print("灰度平均值为：",mean)
# print("标准差为：",stddv)
# cv2.waitKey(0)
# cv2.destroyAllWindows()


import cv2
import numpy as np
import math
img_lena = cv2.imread("Lena.Bmp")
gray_img_lena = cv2.cvtColor(img_lena, cv2.COLOR_BGR2GRAY)
width = gray_img_lena.shape[1]
height = gray_img_lena.shape[0]
A = np.zeros((4, 4), dtype=np.double)  # 4x4变换矩阵
for i in range(4):
    for j in range(4):
        if i == 0:
            A[i][j] = math.sqrt(2/4)*(1/(math.sqrt(2)))*math.cos(math.pi * (j + 0.5) * i / 4)
        else:
            A[i][j] = math.sqrt(2/4)*math.cos(math.pi * (j + 0.5) * i / 4)
A_T = A.transpose()  # 转置矩阵
Mask = np.array([[1, 1, 1, 1],  # 掩膜
                 [1, 1, 1, 0],
                 [1, 0, 0, 0],
                 [0, 0, 0, 0]])
des = np.zeros((width, height), dtype=np.uint8)  # 大矩阵，放iDCT后的图像
for i in range(0, 255-3, 4):
    for j in range(0, 255-3, 4):
        view = gray_img_lena[i:i+4, j:j+4]
        Y1 = np.matmul(A, view)  # 叉乘,DCT变换
        Y = np.matmul(Y1, A_T)
        B2 = np.multiply(Mask, Y)  # 点乘
        I1 = np.matmul(A_T, B2)  # iDCT变换
        I = np.matmul(I1, A)
        des[i:i+4, j:j+4] = I
cv2.imshow('DCT_Image', des)
cv2.waitKey(0)
cv2.destroyAllWindows()


# import cv2
# img_lena = cv2.imread("Lena.Bmp")
# gray_img_lena = cv2.cvtColor(img_lena, cv2.COLOR_BGR2GRAY)
# print(gray_img_lena.shape)
# A = np.zeros((255, 255), dtype=np.uint8)
# for i in range(0, 255-3, 4):
#     for j in range(0, 255-3, 4):
#         view = gray_img_lena[i: i+4, j: j+4]
#         print(view)
#


# cv2.dct()
# A = zeros((8, 8))#生成0矩阵
# shape = gray_img_lena.shape[1]#获取维数
# for i in range(8):
#     for j in range(8):
#         if(i == 0):
#             x = sqrt(1/shape)
#         else:
#             x = sqrt(2/shape)
#         A[i][j] = x*cos(pi*(j+0.5)*i/shape)#与维数相关
#
# A_T = A.transpose()#矩阵转置
# Y1 = matmul(A, gray_img_lena)#矩阵叉乘
# Y = matmul(Y1, A_T)
# print(Y)